package com.epapyrus.plugpdf;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.util.LruCache;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Toast;

import com.epapyrus.plugpdf.core.PDFDocument;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * Created by niuguobin on 2017/9/27.
 */

public class ThumbnailAdapter extends BaseAdapter {
    private Context mContext;
    private List<Bitmap> mBitmap;

    private SimpleReaderControlView controlView;
    private final SparseArray<PointF> mCachePageSizes = new SparseArray();

    public ThumbnailAdapter(Context context,List<Bitmap> mBitmap){
        this.mContext = context;
        this.mBitmap = mBitmap;
    }

    @Override
    public int getCount() {
        return mBitmap.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setSelectionFromTop(int position, int y){

    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_thubnail_list_item, null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.imageView.setImageBitmap(mBitmap.get(position));
        convertView.setTag(holder);
        /*convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Log.i("TAG","============ThumbnailAdapter=============");
                //saveBitmap(mContext,mBitmap.get(position));
                //Toast.makeText(mContext, "点击第"+position+"张缩略图", Toast.LENGTH_SHORT).show();
                *//*Intent intent = new Intent();
                intent.setAction("com.hyzh.pdfview.pdfreader");// 发送广播 ，通知上一个界面更新程序
                intent.putExtra("index", position);// 照片list的下标
                LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
                *//*

            }
        });*/

        return convertView;
    }

    private class ViewHolder {
        private ImageView imageView;

    }

    private static final String SD_PATH = "/sdcard/dskqxt/pic/";
    private static final String IN_PATH = "/dskqxt/pic/";

    /**
     * 随机生产文件名
     *
     * @return
     */
    private static String generateFileName() {
        return UUID.randomUUID().toString();
    }
    /**
     * 保存bitmap到本地
     *
     * @param context
     * @param mBitmap
     * @return
     */
    public static String saveBitmap(Context context, Bitmap mBitmap) {
        String savePath;
        File filePic;
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            savePath = SD_PATH;
        } else {
            savePath = context.getApplicationContext().getFilesDir()
                    .getAbsolutePath()
                    + IN_PATH;
        }
        try {
            filePic = new File(savePath + generateFileName() + ".jpg");
            if (!filePic.exists()) {
                filePic.getParentFile().mkdirs();
                filePic.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(filePic);
            mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
            Toast.makeText(context, "图片保存成功！", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(context, "图片保存失败！", Toast.LENGTH_SHORT).show();
            return null;
        }

        return filePic.getAbsolutePath();
    }

}
